<template>
  <div>
    <navbar title="影像上传" :disabled="true"></navbar>
    <div style="position:relative;top:46px;">
        <van-cell>
          <template slot="title">
              <!-- <span class="van-cell-text">投保地区</span> -->
               <!-- <Upload
                :on-success="handleSuccess"
                :format="['jpg']"
                :max-size="2048"
                :on-format-error="handleFormatError"
                :action="action"
              > -->
              <Upload  
                name='Idcard'
                :data='data1'
                :format="['jpg']"
                :max-size="2048"
                :on-success="handleSuccess"
                :on-remove="handleremove"
                :on-format-error="handleFormatError"
                :action="action">
                 <Button icon="ios-cloud-upload-outline">身份证正面上传</Button>
              </Upload>
                
              <!-- </Upload> -->
          </template>
      </van-cell>
        <van-cell>
          <template slot="title">
              <!-- <span class="van-cell-text">投保地区</span> -->
               <Upload  
                :format="['jpg']"
                :max-size="2048"
                :on-success="handleSuccess"
                :on-format-error="handleFormatError"
                :on-remove="handleremove"
                name='Idcard'
                :data='data1'
                :action="action">
                 <Button icon="ios-cloud-upload-outline">身份证反面上传</Button>
              </Upload>
          </template>
      </van-cell>
        <van-cell>
          <template slot="title">
              <!-- <span class="van-cell-text">投保地区</span> -->
              <Upload  
                :format="['jpg']"
                :max-size="2048"
                :on-success="handleSuccess2"
                :on-format-error="handleFormatError"
                :on-remove="handleremove"
                :multiple='false'
                :data='data2'
                name='checkCarPic'
                :action="action">
                 <Button icon="ios-cloud-upload-outline">验车照片上传</Button>
              </Upload>
          </template>
      </van-cell>
     

      <div class="btnCon">
        <van-button size="large" type="default" @click="update">确定上传</van-button>
      </div>
    </div>
  </div>
</template>

<script>
import navbar from "../components/navbar.vue";

export default {
  components: { navbar },
  data() {
    return {
      action: "",
      baseInfo: window.localStorage.getItem("baseInfo"),
      // baseInfoDto: window.localStorage.getItem("baseInfoDto"),
      files: [],
      data1:{
        name:'Idcard'
      },
      data2:{
        name:'checkCarPic'
      }
    };
  },
  mounted() {
    // console.log(this.action);
    this.action = process.env.API_ROOT + "api/services/ApiImageUpload";
    this.baseInfo = JSON.parse(this.baseInfo)
  },
  methods: {
    onRead(file) {
      console.log(file);
    },
    update() {
      const params = {
        proposalno:
          this.baseInfo.businessApplyNo != ""
            ? this.baseInfo.businessApplyNo
            : this.baseInfo.trafficApplyNo,
        filles: this.files,
        merchantName: "taishan",
        functionName: "ImageUpload"
      };
      this.post(params, "api/services/app/car/ImageUpload").then(res => {
        if(res.data.result.insuranceCode == 0){
            this.$dialog.alert({
              title: "提示",
              message:"上传成功，请重新提交核保",
            }).then(() => {
              // on confirm
                this.$router.push({
                    name: "listAddress",
                    query: {
                    
                    }
                });
            })
        }else{
           this.$dialog.confirm({
              title: "提示",
              message: res.data.result.insuranceMsg,
              confirmButtonText: "确定",
              cancelButtonText: "返回核保",
              closeOnClickOverlay: true
            })
            .then(() => {
              // on confirm
                // this.$router.push({
                //     name: "listAddress",
                //     query: {
                    
                //     }
                // });
            })
            .catch(() => {
              // on cancel
              this.$router.push({
                    name: "listAddress",
                    query: {
                    
                    }
                });
            });
        }
      });
    },
    handleFormatError(file) {
      // this.$dialog
      //   .confirm({
      //     title: "提示",
      //     message: "当前只能上传jpg格式图片",
      //     confirmButtonText: "确定",
      //     cancelButtonText: "取消",
      //     closeOnClickOverlay: true
      //   })
      //   .then(() => {
      //     // on confirm
      //   })
      //   .catch(() => {
      //     // on cancel
      //   });
    },
    handleSuccess(res, file, fileList) {
      console.log(res)
      console.log(file);
      console.log(fileList);
      for (let i = 0; i < fileList.length; i++) {
        this.files.push({
          filetype:'Idcard',
          filePath: fileList[i].response.filePath
        });
      }
      // // console.log(arr)
      console.log(this.files);
    },
    handleSuccess2(res, file, fileList) {
      console.log(res)
      console.log(file);
      console.log(fileList);
      console.log(2)
      for (let i = 0; i < fileList.length; i++) {
        this.files.push({
          filetype:'checkCarPic',
          filePath: fileList[i].response.filePath
        });
      }
      
      // // console.log(arr)
      console.log(this.files);
    },
    handleremove(file, fileList){

    }
  }
};
</script>
<style scoped lang="scss">
.btnCon {
  width: 10rem;
  position: fixed;
  bottom: 0;
  background: #fff;
}
</style>
